package xyz.labradormm.auth.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.web.bind.annotation.ExceptionHandler;
import xyz.labradormm.auth.domain.dto.Result;

/**
 * 全局异常处理类
 */
//@ControllerAdvice
//@RequestMapping("/error")
@Slf4j
public class GlobalExceptionHandler extends BasicErrorController {

    private ErrorAttributes errorAttributes;

    //@Autowired
    public GlobalExceptionHandler(ErrorAttributes errorAttributes) {
        super(errorAttributes, new ErrorProperties());
        this.errorAttributes = errorAttributes;
    }


    /**
     * Service层全局异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        if (e instanceof ApplicationException) {
            log.info("{} at {}", e.getMessage(), e.getStackTrace()[0].toString());
            ApplicationException ex = (ApplicationException) e;
            return new Result(ex.getCode(), ex.getMessage());
        }
        return Result.fail();
    }

}